package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpContactsAddDTO;
import org.jsola.hr.dto.EmpContactsUpdateDTO;
import org.jsola.hr.query.EmpContactsQuery;
import org.jsola.hr.service.IEmpContactsService;
import org.jsola.hr.vo.EmpContactsListVO;
import org.jsola.hr.vo.EmpContactsVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrEmpContactsController")
@RequestMapping(HrConstants.API_PATH + "/v1/empContacts")
public class EmpContactsController implements IEmpContactsControllerAPI {

    @Autowired
    private IEmpContactsService empContactsService;

    @Override
    @PostMapping
    public Result<EmpContactsVO> save(@Valid @RequestBody EmpContactsAddDTO empContactsAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工联系人权限校验

        return Result.success(
                empContactsService.save(empContactsAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpContactsUpdateDTO empContactsUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工联系人权限校验

        int count = empContactsService.update(empContactsUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{empContactsId}")
    public Result<Integer> delete(@PathVariable String empContactsId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工联系人权限校验

        int count = empContactsService.deleteByIds(tokenUser, empContactsId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] empContactsIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工联系人权限校验

        int count = empContactsService.deleteByIds(tokenUser, empContactsIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empContactsId}")
    public Result<EmpContactsVO> findById(@PathVariable String empContactsId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工联系人权限校验

        return Result.success(
            empContactsService.selectById(empContactsId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpContactsListVO>> listAll(@Valid EmpContactsQuery empContactsQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工联系人权限校验

        return Result.success(
            empContactsService.select(empContactsQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<EmpContactsListVO>> page(@Valid EmpContactsQuery empContactsQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            empContactsService.selectPage(empContactsQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @DeleteMapping(value = "/deleteEmpContacts/{empContactsId}")
    public Result<Integer> deleteEmpContacts(@PathVariable String empContactsId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工培训经历权限校验

        Integer count = empContactsService.deleteEmpContacts(tokenUser, empContactsId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


}

